/// <binding ProjectOpened='watch' />
//load the gulp plugins that we use.
var gulp = require('gulp');
var less = require('gulp-less');
var gutil = require("gulp-util");
var watchLess = require('gulp-watch-less');
var sourcemaps = require('gulp-sourcemaps');
var livereload = require('gulp-livereload');
var typescript = require('gulp-typescript');
var replace = require('gulp-replace');
var plumber = require("gulp-plumber");
var del = require("del");
var rimraf = require("rimraf");
var concat = require("gulp-concat");

var paths = {
    js:{
       src:["wwwroot/js/**/*.js","!wwwroot/js/main.js"],
       concatJsDest:"wwwroot/js/main.js"
    },
    typescript: {
        src: ['wwwroot/typescripts/*.ts'],
        outputFolder: 'wwwroot/typescripts/output/',
        outputFileName: "application.js"
    },
    less: {
        src:["wwwroot/less/**/*.less"] ,
        outputFolder: "wwwroot/css/"
    },
    css:{
        src:["wwwroot/css/*.css","!wwwroot/css/main.css"],
        concatCssDest:"wwwroot/css/main.css"
    }
}

gulp.task("clean:js", function (cb) {
    rimraf(paths.js.concatJsDest, cb);
});

gulp.task("clean:css", function (cb) {
    rimraf(paths.less.concatCssDest, cb);
});

gulp.task("clean", ["clean:js", "clean:css"]);

function compileLess(source){
     return source
        .pipe(plumber()) //Util that logs errors instead of things breaking etc.
        .pipe(sourcemaps.init()) //Prepare to handle source mapping for the current source.
        .pipe(less()) //Compile less to css and hook into sourcemaps
        .pipe(sourcemaps.write()) //Write the output of the source maps into the css files
        .pipe(gulp.dest(paths.less.outputFolder)) //Write the completed css file to the output folder
        .pipe(livereload()); //Reload the css (without reloading the page!) in browsers attached to the livereload server. https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei?hl=en
    //Due to a bug you may have to make sure to have the elements tab of the devtools open for this to work correctly in chrome: https://github.com/livereload/livereload-extensions/issues/26
}

gulp.task("less", function () { //Builds less into css and reloads any browsers attached to the livereload server.
   return compileLess(gulp.src(paths.less.src));
});


var tsProject = typescript.createProject({
    target: "ES5",
    out: paths.typescript.outputFileName
});

gulp.task('typescript',function () {
    return gulp.src(paths.typescript.src)
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(typescript(tsProject))
        .pipe(sourcemaps.write(".", {
            includeContent: false,
            sourceRoot: "/"
        }))
        .pipe(gulp.dest(paths.typescript.outputFolder))
        .pipe(livereload());
});

gulp.task("watch", ["build"], function () { //Watches source files and rebuilds them on demand.
    livereload.listen();
    gulp.watch([paths.typescript.src], ["typescript"], function () { });

    gulp.watch(["**/*.cshtml", "**/*.html"], function (event) {
        livereload.reload(event.path);
    });

    gulp.watch(paths.js.src,["concat:js"],function(){});

    gulp.watch(paths.less.src,["concat:css"],function(){});
    //return compileLess(watchLess(paths.less.src));
});

gulp.task("concat:js", function () {
    return gulp.src(paths.js.src)
        .pipe(concat(paths.js.concatJsDest))
        .pipe(gulp.dest("."))
        .pipe(livereload());
});

gulp.task("concat:css", function () {
    return gulp.src(paths.css.src)
        .pipe(concat(paths.css.concatCssDest))
        .pipe(gulp.dest("."))
        .pipe(liveload());
});

gulp.task("concat", ["concat:js", "concat:css"]);

gulp.task("build", ["less", "typescript","concat"]); //Task executed when building the visual studio solution from a build event script.